/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerPeer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import hashChunk.*;

/**
 *
 * @author Chickyky
 */
public class ShareFile {

    private JList m_ListGUI;
    private ArrayList<File> m_ListShare;
    private ArrayList<Chunk> m_ListChunkShare;
    private DefaultListModel listModel;

    public DefaultListModel getListModel() {
        return listModel;
    }

    public void setListModel(DefaultListModel listModel) {
        this.listModel = listModel;
    }

    public ArrayList<Chunk> getM_ListChunkShare() {
        return m_ListChunkShare;
    }

    public void setM_ListChunkShare(ArrayList<Chunk> m_ListShare) {
        this.m_ListChunkShare = m_ListShare;
    }

    public ArrayList<File> getM_ListShare() {
        return m_ListShare;
    }

    public void setM_ListShare(ArrayList<File> m_ListShare) {
        this.m_ListShare = m_ListShare;
    }

    public JList getM_ListGUI() {
        return m_ListGUI;
    }

    public void setM_ListGUI(JList m_ListGUI) {
        this.m_ListGUI = m_ListGUI;
    }

    public ShareFile(JList m_ListGUI) {
        this.m_ListGUI = m_ListGUI;
    }

    public void readFileShare() {
        if (this.m_ListShare == null) {
            this.m_ListShare = new ArrayList<File>();
            this.m_ListChunkShare = new ArrayList<Chunk>();
        } else {
            this.m_ListShare.clear();
            this.m_ListChunkShare.clear();
        }

        File folderDownload = new File("share\\download");
        File folderLocal = new File("share\\local");

        String fileName;
        String[] folders = folderDownload.list();
        for (String folder : folders) {
            File filesInFolder = new File("share\\download\\" + folder);
            File[] files = filesInFolder.listFiles();
            for (File file : files) {
                fileName = file.getName();
                Chunk chunk = new Chunk();
                chunk.setM_FileName(fileName);
                try {
                    chunk.setM_HashValue(ProcessTorrent.hashCode(file));
                } catch (NoSuchAlgorithmException ex) {
                    ex.printStackTrace();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                m_ListChunkShare.add(chunk);
                m_ListShare.add(file);
                System.out.println(chunk.getM_FileName());
                System.out.println(chunk.getM_HashValue());
                System.out.println();
            }
        }

        folders = folderLocal.list();
        for (String folder : folders) {
            File filesInFolder = new File("share\\local\\" + folder);
            File[] files = filesInFolder.listFiles();
            for (File file : files) {
                fileName = file.getName();
                Chunk chunk = new Chunk();
                chunk.setM_FileName(fileName);
                try {
                    chunk.setM_HashValue(ProcessTorrent.hashCode(file));
                } catch (NoSuchAlgorithmException ex) {
                    ex.printStackTrace();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                m_ListChunkShare.add(chunk);
                m_ListShare.add(file);
                System.out.println(chunk.getM_FileName());
                System.out.println(chunk.getM_HashValue());
                System.out.println();
            }
        }
    }

    public void updateListShare(File source) throws NoSuchAlgorithmException {
        m_ListShare.add(source);

        ProcessTorrent procTorrent = new ProcessTorrent();
        procTorrent.splitFile(source);

        this.updateListChunkShareOntoGUI();
    }

    public void updateListChunkShareOntoGUI() {
        this.readFileShare();
        if (getListModel() == null) {
            setListModel(new DefaultListModel());
            this.m_ListGUI.setModel(getListModel());
        } else {
            getListModel().clear();
        }
        for (File item : this.getM_ListShare()) {
            getListModel().addElement(item.getName());
        }
    }

    public boolean checkChunkInList(Chunk chunk) {
        boolean check = false;

        for (Chunk _chunk : getM_ListChunkShare()) {
            if(_chunk.getM_FileName().equals(chunk.getM_FileName())
                    && _chunk.getM_HashValue().equals(chunk.getM_HashValue())) {
                check = true;
                break;
            }
        }

        return check;
    }
}
